//
//  KDFocusListCell.m
//  YuQingXiaoFang
//
//  Created by wangdongdong on 18/6/15.
//  Copyright © 2018年 KDTech. All rights reserved.
//

#import "KDFocusListCell.h"
#import "KDFocusListCellModel.h"
#import "KDFocusListCellModelFrame.h"
@interface KDFocusListCell()
    @property (weak, nonatomic)  UIImageView *leftView;
    @property (weak, nonatomic)  UILabel *titleLb;
    @property (weak, nonatomic)  UILabel *timeLb;
    @property (weak, nonatomic)  UILabel *sourceLb;
    @property (weak, nonatomic)  UILabel *contentLb;
    @property (weak, nonatomic)  UIImageView *star1View;
    @property (weak, nonatomic)  UIImageView *star2View;
    @property (weak, nonatomic)  UIImageView *star3View;
    @property (weak, nonatomic)  UIButton *cancelBtn;

@property (nonatomic, retain) NSDateFormatter *fmt;
@end
@implementation KDFocusListCell


- (NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
        _fmt.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    }
    return _fmt;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *cellId =@"KDFocusListCell";
    
    
    KDFocusListCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    
    if(cell==nil){
        cell=[[KDFocusListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.backgroundColor = BackGroundColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    cell.similarBtn.layer.cornerRadius = 3.0f;
    //    cell.similarBtn.clipsToBounds = YES;
    //    cell.similarBtn.layer.borderWidth = 0.5;
    //    [cell.reportBtn setImage:[UIImage imageNamed:@"已上报"] forState:UIControlStateDisabled];
    
    
    return cell;
}


-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupSubviews];
     }

    return self;
}


-(void) setupSubviews{
    
    UILabel *titleLb=[[UILabel alloc] init];
    [self.contentView addSubview:titleLb];
    _titleLb=titleLb;
    
    UILabel *timeLb=[[UILabel alloc] init];
    [self.contentView addSubview:timeLb];
    _timeLb=timeLb;
    
    UILabel *sourceLb=[[UILabel alloc] init];
    [self.contentView addSubview:sourceLb];
    _sourceLb=sourceLb;
    
    UILabel *contentLb=[[UILabel alloc] init];
    contentLb.numberOfLines=2;
    [self.contentView addSubview:contentLb];
    _contentLb=contentLb;
    
    UIButton *cancelBtn=[[UIButton alloc] init];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:cancelBtn];
    _cancelBtn=cancelBtn;
    
    UIImageView *star1View= [[UIImageView alloc] init];
    [self.contentView addSubview:star1View];
    _star1View=star1View;
    
    UIImageView *star2View= [[UIImageView alloc] init];
    [self.contentView addSubview:star2View];
    _star2View=star2View;
    
    UIImageView *star3View= [[UIImageView alloc] init];
    [self.contentView addSubview:star3View];
    _star3View=star3View;
}

-(void)setModelFrame:(KDFocusListCellModelFrame *)modelFrame{
    _modelFrame=modelFrame;
    
    [self setupModelData];
    
}

-(void) setupModelData{
    KDFocusListCellModel *model= self.modelFrame.model;
    
    //标题
    self.titleLb.text=model.newstitle;
    self.titleLb.font=KDNewsCellModelTitleFont;
    self.titleLb.frame=self.modelFrame.titleLabelF;
    
    //日期
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.attentTime/ 1000.0];
    self.timeLb.text = [self.fmt stringFromDate:date];
    self.timeLb.font=KDNewsCellModelTimeFont;
    self.timeLb.frame=self.modelFrame.timeLabelF;
    
    //来源或作者
    
    
        self.sourceLb.text = model.newssource;
    
    self.sourceLb.font=KDNewsCellModelTimeFont;
    self.sourceLb.frame=self.modelFrame.sourceLabelF;
    
    //内容
    self.contentLb.text = model.newssummary;
        self.contentLb.font=KDNewsCellModelContentFont;
        self.contentLb.frame=self.modelFrame.contentLabelF;
        [self.contentLb sizeToFit];
        
        
    //预警级别
    if(model.newsLevel==0 || model.newsLevel==1 || model.newsLevel==2 || model.newsLevel==3){
        self.star1View.image=[UIImage imageNamed:@"收藏选中"];
        self.star1View.frame=self.modelFrame.star1ViewF;
    }
    if(model.newsLevel==2 || model.newsLevel==3){
        self.star2View.hidden=NO;
        self.star2View.image=[UIImage imageNamed:@"收藏选中"];
        self.star2View.frame=self.modelFrame.star2ViewF;
    }else{
        self.star2View.hidden=YES;
    }
    if(model.newsLevel==3){
        self.star3View.hidden=NO;
        self.star3View.image=[UIImage imageNamed:@"收藏选中"];
        self.star3View.frame=self.modelFrame.star3ViewF;
    }else{
        self.star3View.hidden=YES;
    }
    
    //取消关注
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font=KDNewsCellModelButtonFont;
    self.cancelBtn.backgroundColor=[UIColor blueColor];
    self.cancelBtn.frame=self.modelFrame.cancelButtonF;
    
    
    
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark － 事件点击区
/**  取消关注按钮被点击了 */
- (IBAction)cancelBtnClick:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(subscribCell:cancelBtnClick:)]) {
        [_delegate subscribCell:self cancelBtnClick:sender];
    }
}

@end
